package kr.hkit.powerjava.chapter21.submit; // 챕터21 프로그래밍4

class NegativeBalanceException extends Exception {
	public NegativeBalanceException(String message) {
		super(message);
	}
}

class BankAccount {

	int balance ; // 잔액

	public int deposit(int amount) { // 입금
		return balance += amount;
	}

	public int withdraw(int amount) throws NegativeBalanceException { // 출금
		if (amount > balance) {
			throw new NegativeBalanceException("잔고가 부족합니다.");
		} else {
			balance -= amount;
			return amount;
		}
	}
}


public class BankAccountTest {
	public static void main(String[] args) {
		
		BankAccount b = new BankAccount();
		System.out.println(b.deposit(5000)+"원이 입금되었습니다."); //입금액
		int amount = 6000; //출금액
		
		try {
			int result = b.withdraw(amount);
			System.out.println(result+ "이 출금되었습니다."); //정상출금이면 원래대로 출력
		} catch (NegativeBalanceException e1) {
			System.out.println(e1.getMessage()); //잔고가 부족하다면 예외발생.
			e1.printStackTrace();
		}
		
		
	}
}
